﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentManagementSystem.StudentManagementSystem;

namespace StudentManagementSystem
{
    internal class Program
    {
        static void Main(string[] args)
        {
            DatabaseManager dbManager = new DatabaseManager();
            Logger logger = new Logger();

            while (true)
            {
                Console.WriteLine("Enter command:");
                string command = Console.ReadLine();
                string[] parts = command.Split(' ');

                switch (parts[0])
                {
                    case "add":
                        if (parts[1] == "school")
                        {
                            School newSchool = new School { Name = parts[2], Address = parts[3] };
                            dbManager.InsertSchool(newSchool);
                            logger.Log("Add", $"Added School: {newSchool.Name}");
                        }
                        else if (parts[1] == "class")
                        {
                            Class newClass = new Class { Name = parts[2] };
                            dbManager.InsertClass(newClass);
                            logger.Log("Add", $"Added Class: {newClass.Name}");
                        }
                        else if (parts[1] == "student")
                        {
                            Student newStudent = new Student { Name = parts[2], Age = int.Parse(parts[3]), Gender = parts[4], Class = parts[5] };
                            dbManager.InsertStudent(newStudent);
                            logger.Log("Add", $"Added Student: {newStudent.Name}");
                        }
                        break;

                    case "delete":
                        if (parts[1] == "school")
                        {
                            dbManager.DeleteSchool(parts[2]);
                            logger.Log("Delete", $"Deleted School: {parts[2]}");
                        }
                        else if (parts[1] == "class")
                        {
                            dbManager.DeleteClass(parts[2]);
                            logger.Log("Delete", $"Deleted Class: {parts[2]}");
                        }
                        else if (parts[1] == "student")
                        {
                            dbManager.DeleteStudent(parts[2]);
                            logger.Log("Delete", $"Deleted Student: {parts[2]}");
                        }
                        break;

                    case "update":
                        if (parts[1] == "school")
                        {
                            School updatedSchool = new School { Name = parts[3], Address = parts[4] };
                            dbManager.UpdateSchool(parts[2], updatedSchool);
                            logger.Log("Update", $"Updated School: {parts[2]}");
                        }
                        else if (parts[1] == "class")
                        {
                            Class updatedClass = new Class { Name = parts[3] };
                            dbManager.UpdateClass(parts[2], updatedClass);
                            logger.Log("Update", $"Updated Class: {parts[2]}");
                        }
                        else if (parts[1] == "student")
                        {
                            Student updatedStudent = new Student { Name = parts[3], Age = int.Parse(parts[4]), Gender = parts[5], Class = parts[6] };
                            dbManager.UpdateStudent(parts[2], updatedStudent);
                            logger.Log("Update", $"Updated Student: {parts[2]}");
                        }
                        break;

                    case "query":
                        if (parts[1] == "school")
                        {
                            School school = dbManager.QuerySchool(parts[2]);
                            Console.WriteLine($"School Name: {school.Name}, Address: {school.Address}");
                        }
                        else if (parts[1] == "class")
                        {
                            Class @class = dbManager.QueryClass(parts[2]);
                            Console.WriteLine($"Class Name: {@class.Name}");
                        }
                        else if (parts[1] == "student")
                        {
                            Student student = dbManager.QueryStudent(parts[2]);
                            Console.WriteLine($"Student Name: {student.Name}, Age: {student.Age}, Gender: {student.Gender}, Class: {student.Class}");
                        }
                        break;

                    case "log":
                        logger.DisplayLogs();
                        break;

                    case "exit":
                        return;

                    default:
                        Console.WriteLine("Unknown command");
                        break;
                }
            }
        }
    }
}
